package com.topwalk.iwp.multiService.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.topwalk.iwp.multiService.dao.MultiServiceDao;
import com.topwalk.ws.multiService.PeopleServiceItem;
import com.topwalk.ws.multiService.PeopleWhere;

/**
 * Servlet implementation class GetPeopInfo
 */
public class GetPeopInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		PeopleWhere where = buildPeopleWhere(req);
		String dishi = buildDishi(req);
		req.setAttribute("data",MultiServiceDao.getPeopleData(null, dishi, where, null, null));

		req.getRequestDispatcher("people_list.jsp").forward(req, resp);
	}

	private String buildDishi(HttpServletRequest req) {
		String dishi = (String) req.getParameter("dishi");
		if (null != dishi) {
			@SuppressWarnings("unchecked")
			List<PeopleServiceItem> peopleServiceList = (List<PeopleServiceItem>) req
					.getSession().getAttribute("serviceList");
			if (null != peopleServiceList && peopleServiceList.size() > 0) {
				peopleServiceList = MultiServiceDao.getPeopleServiceList(null);
				req.getSession().setAttribute("serviceList", peopleServiceList);
			}
			if (null != peopleServiceList && peopleServiceList.size() > 0) {
				for (PeopleServiceItem item : peopleServiceList) {
					if (item.getIdentity().equals(dishi)) {
						req.setAttribute("dishi_name", item.getServiceRemark());
						break;
					}
				}
			}
		}
		return dishi;
	}

	private PeopleWhere buildPeopleWhere(HttpServletRequest req) {
		PeopleWhere rs = new PeopleWhere();
		if (null != req.getParameter("xb"))
			rs.setZBMC((String) req.getParameter("xb"));
		if (null != req.getParameter("sfzh"))
			rs.setSFZH((String) req.getParameter("sfzh"));
		if (null != req.getParameter("xxzz"))
			rs.setXXZZ((String) req.getParameter("xxzz"));
		if (null != req.getParameter("jgmc"))
			rs.setJGMC((String) req.getParameter("jgmc"));
		if (null != req.getParameter("xm"))
			rs.setNAME((String) req.getParameter("xm"));

		return rs;
	}
}
